package com.dd.consul_consumer_feign.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.dd.consul_consumer_feign.dto.UserDto;
import com.dd.consul_consumer_feign.service.FeignService;

@Api
@RequestMapping("/feign")
@RestController
public class FeignController {

    @Resource
    private FeignService feignService;
    
    @ApiOperation(value="获取用户列表", notes="")
    @RequestMapping("/list")
    public List<UserDto> list() {
    	return feignService.list();
    }
    
    @ApiOperation(value="查询用户", notes="根据User对象创建用户")
    @RequestMapping("/getUser/{id}")
    public UserDto getUser(@PathVariable Long id) {
    	return feignService.getUser(id);
    }
    
    @ApiOperation(value="查询用户", notes="根据User对象创建用户")
    @RequestMapping("/getUser2")
    public UserDto getUser2(@RequestParam("id") Long id) {
    	return feignService.getUser(id);
    }
    
	@ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "UserDto")
    @RequestMapping("/add")
    public boolean add(@RequestBody UserDto user) {
    	return feignService.add(user);
    }    
 
	@ApiOperation(value="修改用户", notes="根据User对象修改用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "UserDto")
    @RequestMapping("/update")
    public boolean update(@RequestBody UserDto user) {
    	return feignService.update(user);
    } 
 
	@ApiOperation(value="删除用户", notes="根据ID删除用户")
	@PostMapping("/delete/{id}")
    public boolean delete(@PathVariable Long id) {
    	return feignService.delete(id);
    }
	
    @ApiOperation(value="查询用户", notes="根据User对象创建用户")
    @RequestMapping("/getUserTimeOut/{id}")
    public UserDto getUserTimeOut(@PathVariable Long id) {
    	try {
    		System.out.println("=====new======="+id);
    		return feignService.getUserTimeOut(id);
		} catch (Exception e) {
			e.printStackTrace();
			return new UserDto(-2L,e.getMessage(),e.getMessage());
		} 
    }

}
